﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using System.Collections.Generic;
using System.Drawing.Design;
using System.Runtime.InteropServices.WindowsRuntime;

namespace CADTool
{
    internal class SelectTool
    {
        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <param name="message">提示信息</param>
        /// <returns></returns>
        public static Entity SelectEntity(string message)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Entity ent = null;
            PromptEntityResult per = ed.GetEntity(message);
            if (per.Status == PromptStatus.OK)
            {
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    ent = (Entity)trans.GetObject(per.ObjectId, OpenMode.ForWrite);
                    trans.Commit();
                }
            }
            return ent;
        }
        /// <summary>
        /// 获取实体集合
        /// </summary>
        /// </returns>List<Entity></returns>
        public static List<Entity> SelectEntities()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            //获取PickFirst 选择集
            PromptSelectionResult psr;
            SelectionSet sSt;
            //清空PickFirst选择集
            ObjectId[] idarryEmpty = new ObjectId[0];
            ed.SetImpliedSelection(idarryEmpty);
            //请求从图形区域选择对象
            psr = ed.GetSelection();
            //如果提示状态OK，表示已选择对象
            if (psr.Status == PromptStatus.OK)
            {
                sSt = psr.Value;
                //List<ObjectId> objectId=sSt.GetObjectIds().ToList;
                List<Entity> ents = new List<Entity>();
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    foreach (SelectedObject item in sSt)
                    {
                        Entity ent = (Entity)trans.GetObject(item.ObjectId, OpenMode.ForRead);
                        ents.Add(ent);
                    }
                }
                return ents;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获取单行文字集合
        /// </summary>
        /// <param name="message">提示</param>
        /// <returns>List<DBText></returns>
        public static List<DBText> SelectDBtext(string message)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            List<DBText> txts = new List<DBText>();
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage(message);
            //创建一个TypedValue数组来定义过滤条件
            TypedValue[] typedValue = new TypedValue[1];
            typedValue.SetValue(new TypedValue((int)DxfCode.Start, "Text"), 0);
            //将过滤器条件赋值给SelectionFilter对象
            SelectionFilter selectionFilter = new SelectionFilter(typedValue);
            //请求用户在图形区域选择对象
            PromptSelectionResult psr = ed.GetSelection(selectionFilter);
            SelectionSet selectionSet;
            if (psr.Status == PromptStatus.OK)
            {
                selectionSet = psr.Value;
                if (selectionSet == null) return null;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    foreach (SelectedObject item in selectionSet)
                    {
                        DBText txt = (DBText)trans.GetObject(item.ObjectId, OpenMode.ForRead);
                        txts.Add(txt);
                    }
                }
            }
            return txts;
        }
        /// <summary>
        /// 选择集合
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <returns>返回T类型集合</returns>
        public static List<T> SelectEntities<T>()
        {
            var list = new List<T>();
            var doc = Application.DocumentManager.MdiActiveDocument;
            var r1 = doc.Editor.GetSelection();
            if (r1.Status != PromptStatus.OK) return list;
            using (var tr = doc.Database.TransactionManager.StartTransaction())
            {
                foreach (var id in r1.Value.GetObjectIds())
                {
                    if (tr.GetObject(id, OpenMode.ForRead) is T tent)
                        list.Add(tent);
                }
            }
            return list;
        }
        /// <summary>
        /// 选择集合
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="promptSelectionOptions">提示</param>
        /// <returns>返回T类型的集合</returns>
        public static List<T> SelectEntities<T>(PromptSelectionOptions promptSelectionOptions)
        {
            var list = new List<T>();
            var doc = Application.DocumentManager.MdiActiveDocument;
            var r1 = doc.Editor.GetSelection(promptSelectionOptions);
            if (r1.Status != PromptStatus.OK) return list;
            using (var tr = doc.Database.TransactionManager.StartTransaction())
            {
                foreach (var id in r1.Value.GetObjectIds())
                {
                    if (tr.GetObject(id, OpenMode.ForRead) is T tent)
                        list.Add(tent);
                }
            }
            return list;
        }

    }
}
